package com.dys.teststreamapi11;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @className: com.dys.teststreamapi11.StreamAPITest
 * @description: 结果收集到集合中
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/09/01 下午4:39
 */
public class StreamAPITest10 {
    /**
     * 这是一个main()方法,是应用程序的入口
     *
     * @param args 方法的形参列表,是一个字符串数组,包含了从命令行传递给程序的参数
     */
    public static void main(String[] args) {
        streamToArray();
    }

    /**
     * 收集到集合
     */
    private static void streamCollection() {
        // 收集到List集合
        List<String> collect1 = Stream.of("aa", "bb", "cc", "dd", "aa").collect(Collectors.toList());
        System.out.println(collect1);

        // 收集到Set集合
        Set<String> collect2 = Stream.of("aa", "bb", "cc", "dd", "aa").collect(Collectors.toSet());
        System.out.println(collect2);

        // 如果需要获取的类型为具体的实现，比如：ArrayList HashSet
        ArrayList<String> collect3 = Stream.of("aa", "bb", "cc", "dd", "aa")
                // .collect(Collectors.toCollection(()->new ArrayList<>()));
                .collect(Collectors.toCollection(ArrayList::new));
        System.out.println(collect3);

        HashSet<String> collect4 = Stream.of("aa", "bb", "cc", "dd", "aa")
                .collect(Collectors.toCollection(HashSet::new));

        System.out.println(collect4);
    }

    /**
     * 结果收集到数组中
     */
    private static void streamToArray() {
        Object[] objectArray = Stream.of("aa", "bb", "cc", "dd", "aa").toArray();
        System.out.println(Arrays.toString(objectArray));

        System.out.println("-------------------------------");
        String[] array = Stream.of("aa", "bb", "cc", "dd", "aa")
                .toArray(String[]::new);
        System.out.println(Arrays.toString(array));
    }

}
